/**
 * 
 */
package xx.services.teach;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import xx.services.Passport;
import xx.services.util.Constants;
import xx.teach.entity.Answer;
import xx.teach.entity.Job;

/**
 * @author Administrator 2014-3-22
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/teach/job")
public class JobRest extends Passport
{

    /**
     * @param request
     */
    public JobRest(@Context
    HttpServletRequest request)
    {
        super(request);
    }

    /**
     * 根据jobId获取指定的job，同时获取job的（如果有的话）
     * 
     * @param jobId
     * @return
     */
    @GET
    @Path("/{job_id}")
    public String getJobById(@PathParam("job_id")
    long jobId)
    {
        try
        {
            Job job = new Job(qr, owner);
            job.setJobId(jobId).load();

            Answer answer = new Answer(qr, owner);
            List<Answer> answerList = answer.setJobId(jobId).getAnswerByJobId();
            Map<String, Object> answerMap = new HashMap<String, Object>();
            answerMap.put("answerlist", answerList);

            return ok(job, answerMap);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 根据type和groupId获取job列表
     * 
     * @param groupId
     * @param jobType
     * @return
     */
    @GET
    @Path("/group/{group_id}/type/{type}")
    public String getJobByGroupIdAndType(@PathParam("group_id")
    long groupId, @PathParam("type")
    int jobType)
    {
        try
        {
            Job job = new Job(qr, owner);
            List<Job> jobList = job.setGroupId(groupId).setType(jobType).getJobByGroupIdAndType();
            return ok(jobList);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }
    
    @POST
    @Path("/del")
    public String delJobByJobId(@FormParam("job_id") long jobId){
    	try {
    		Job job = new Job(qr, owner);
			job.setJobId(jobId).delete();
			  return ok("delete success");
		} catch (SQLException e) {
			e.printStackTrace();
			 return error("delete error");
		}
    	
    }
}
